package com.huanong.it.xwweibo.authority.common.handler;

import com.huanong.it.xwweibo.authority.common.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class CacheClearLogoutHandler implements LogoutHandler {
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) {
        if(authentication != null){
            UserDetails userDetails = (UserDetails) authentication.getPrincipal();
            // 清除缓存
            redisUtil.clearUserCache(userDetails.getUsername());
        }

    }
}
